package com.job;

import com.alibaba.druid.pool.DruidDataSource;
import com.database.db.BaseDao;
import com.database.db.ConnectionFactory;
import com.database.utils.PropertyUtils;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-04-19.
 *
 * @author chouFy
 */
public class TestDao extends BaseDao {

    public TestDao() {

        String driverClassName = PropertyUtils.getValue("driverClassName");
        String jdbcurl = PropertyUtils.getValue("jdbcurl");
        String username = PropertyUtils.getValue("username");
        String password = PropertyUtils.getValue("password");
        DruidDataSource druidDataSource = ConnectionFactory.getInstance().getDruidDataSource(username, password, jdbcurl, driverClassName);
        setDruidDataSource(druidDataSource);
    }


    public int save() {

        Random random = new Random();
        int i = random.nextInt(3);

        String substring = UUID.randomUUID().toString().toUpperCase().replace("-", "").substring(0, 10);
        String sql = "insert into resume (name,sex,phone,address,education ,state) " +
                "values (?,?,?,?,?,'未归档')";
        return executeSql(sql, new Object[]{substring,substring,substring,substring,i});
    }



    public Map<String,Object> getData(){
        String sql = "select * from resume where state = ? limit 1";
        return  queryForMap(sql, new Object[]{"未归档"});
    }


    public Map<String,Object> getData(String education){
        String sql = "select * from resume where education=? and  state = ? limit 1";
        return  queryForMap(sql, new Object[]{education, "未归档"});
    }


    public int update(long id){
        String sql =  "update resume set state = '已归档' where id = ? ";
        return executeSql(sql, new Object[]{id});
    }


    public int backData(long id){
        String sql = "insert into resume_bak select * from resume where id = ? ";
        return executeSql(sql, new Object[]{id});
    }
}
